package hht.dragon.blog.controller;

import com.github.pagehelper.PageInfo;
import hht.dragon.blog.common.api.ReturnData;
import hht.dragon.blog.common.api.utils.Returns;
import hht.dragon.blog.model.Article;
import hht.dragon.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * 首页Controller.
 * User: huang
 * Date: 18-6-23
 */
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private ArticleService articleService;

    /**
     * 分页文章信息.
     * @return
     */
    @GetMapping("/article/{pageNum}")
    public Mono<ReturnData<PageInfo<Article>>> indexNewArticle(@PathVariable("pageNum") int pageNum, String typeId) {
        int id = 0;
        if (typeId != null && typeId.length() > 0) {
            id = Integer.parseInt(typeId);
        }
        PageInfo<Article> info = articleService.pageAllArticles(pageNum, id);
        return Returns.newMonoReturnData(200, info);
    }

    /**
     * 首页推荐文章.
     * @return
     */
    @GetMapping("/article/hot")
    public Mono<ReturnData<List<Article>>> indexHotArticle() {
        List<Article> articles = articleService.hotArticle();
        return Returns.newMonoReturnData(200, articles);
    }

}
